package com.arceus.leetcode.dytedance;

/**
 * @author : iron
 * @version : 1.0.0
 * @date : 11:36 AM 2018/12/27
 */

public class T2 {

    public static void main(String[] args) {
        String[] str = new String[]{"aabccsd", "a", "aabcaacsdq"};
        System.out.println(longestCommonPrefix(str));
    }

    public static String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        String front = "";

        String first = strs[0];

        char[] chars = first.toCharArray();

        int index = 0;
        int size = 0;

        for (char a : chars) {
            for (int i = 0; i < strs.length; i++) {
                if (strs[i].length() >= index + 1) {
                    if (strs[i].charAt(index) == a) {
                        size++;
                    }
                }
            }
            if (size == strs.length) {
                index++;
                front += a;
                size = 0;
            } else if (size != 0) {
                return front;
            }
        }

        return front;
    }
}
